<script setup>
  import {ref} from "vue"
  
  // 导入axios用于发送请求
  import axios from "axios"

  // 创建响应式数据用于记录加载状态
  const isLoading = ref(false)

  // 1.定义响应式数组
  const newsList = ref([])

  getNewsList()  
  
  // 2.在组件加载伊始发送ajax请求获取数据
  async function getNewsList() {
    // 发送请求前，更改加载状态
    isLoading.value = true

    const resp = await axios({
      url: 'http://localhost:4000/api/news',
      method: "GET"
    })

    newsList.value = resp.data.data
    // 发送请求结束后，同步加载状态
    isLoading.value = false
  }
</script>

<template>
<div class="app" v-load="isLoading">
  <div class="item"
   v-for="item in newsList"
   :key="item.id">
    <div class="text">
      <p class="title">{{ item.title }}</p>
      <p>
        <span class="source">
          {{ item.source }}
        </span>
        <span class="source">
          {{ item.time }}
        </span></p>
    </div>
    <div class="pic">
      <img :src="item.img" :alt="item.title">
    </div>
  </div>
</div>
</template>

<style>
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }
  .app {
    width: 900px;
    margin: 50px auto;
  }
  .loading::before {
    z-index: 999;
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: #fff url('./img/loading.gif') no-repeat center;
    background-size: auto;
  }
  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 200px;
    margin: 10px 0;
  }
  .text {
    height: 160px;
    width: 600px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .title {
    font-size: 20px;
    font-weight: bolder;
  }
  .source {
    margin-right: 2em;
    color: #bbb;
    font-size: 16px;
  }
</style>